package com.thesis.manage.service.impl;

import com.github.crab2died.ExcelUtils;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.mongodb.impl.AbstractMongoServiceImpl;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Grade;
import com.thesis.manage.domain.Student;
import com.thesis.manage.domain.StudentTeacher;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.service.*;
import com.thesis.manage.service.model.AnswerStateEnum;
import com.thesis.manage.service.model.TimeTypeEnum;
import com.thesis.manage.service.module.excel.GradeExcel;
import com.thesis.manage.service.module.vo.GradeVO;
import com.thesis.manage.service.module.vo.TimeVO;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author php
 * @date 2018/4/5
 */
@Service
public class GradeServiceImpl extends AbstractMongoServiceImpl<Grade> implements GradeService {

    @Resource
    private StudentTeacherService studentTeacherService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private StudentService studentService;
    @Resource
    private ThesisService thesisService;

    @Override
    protected Class<Grade> getEntityClass() {
        return Grade.class;
    }

    @Override
    public void addGrade(Teacher teacher, String studentId, Double reviewGrade, Double answerGrade) {
        //时间验证
        Result result = thesisService.checkOpenApplication(TimeTypeEnum.ANSWER.getValue());
        TimeVO timeVO = (TimeVO) result.getData();
        if (timeVO.getType().intValue() == AnswerStateEnum.NO_OPEN.getValue()) {
            throw new CommonException("no open", "答辩阶段未开启");
        }
        if (timeVO.getType().intValue() == AnswerStateEnum.END.getValue()) {
            throw new CommonException("end", "答辩阶段已结束");
        }
        //查询是否已有成绩
        Grade grade = new Grade();
        grade.setStudentNumber(studentId);
        Grade one = this.findByOne(grade);

        grade.setReviewGrade(reviewGrade);
        grade.setAnswerGrade(answerGrade);
        grade.setTeacherName(teacher.getName());
        if (one != null) {
            //修改
            grade.setId(one.getId());
            this.update(grade);
        } else {
            //添加
            this.insert(grade);
        }
    }

    @Override
    public Grade findByStudentId(String studentId) {
        Grade grade = new Grade();
        grade.setStudentNumber(studentId);
        return this.findByOne(grade);
    }

    @Override
    public void importGradeInfo(Teacher teacher, MultipartFile file) {
        //时间验证
        Result result = thesisService.checkOpenApplication(TimeTypeEnum.ANSWER.getValue());
        TimeVO timeVO = (TimeVO) result.getData();
        if (timeVO.getType().intValue() == AnswerStateEnum.NO_OPEN.getValue()) {
            throw new CommonException("no open", "答辩阶段未开启");
        }
        if (timeVO.getType().intValue() == AnswerStateEnum.END.getValue()) {
            throw new CommonException("end", "答辩阶段已结束");
        }
        if (file.isEmpty()) {
            throw new CommonException("file is empty", "文件为空");
        }
        try {
            List<GradeExcel> gradeExcels = ExcelUtils.getInstance().readExcel2Objects(file.getInputStream(), GradeExcel.class);
            if (CollectionUtils.isNotEmpty(gradeExcels)) {
                for (GradeExcel gradeExcel : gradeExcels) {
                    this.addGrade(teacher, gradeExcel.getStudentNumber(), gradeExcel.getReviewGrade(), gradeExcel.getAnswerGrade());
                }
            }
        } catch (Exception e) {
            throw new CommonException("file import failed", "文件导入失败");
        }

    }

    @Override
    public GradeVO findGradeByStudentId(String studentNumber) {
        Grade grade = this.findByStudentId(studentNumber);
        if (grade == null) {
            return null;
        }
        StudentTeacher studentTeacher = this.studentTeacherService.findByStudentNumber(studentNumber);
        Teacher teacher = this.teacherService.findByTeacherNumber(studentTeacher.getTeacherNumber());
        if (teacher == null) {
            throw new CommonException("you no guide_teacher", "你暂无指导教师");
        }
        GradeVO gradeVO = new GradeVO();
        try {
            PropertyUtils.copyProperties(gradeVO, grade);
            gradeVO.setGuideTeacherName(teacher.getName());
        } catch (Exception e) {
            throw new CommonException("object convert failed", "对象转换失败");
        }
        return gradeVO;
    }

    @Override
    public void addGradeByTeacher(Long studentId, Double teacherGrade, String message) {
        Student student = this.studentService.findById(studentId);
        if (student == null) {
            throw new CommonException("student not exist", "学生不存在");
        }
        //查询是否已有成绩
        Grade grade = new Grade();
        grade.setStudentNumber(student.getStudentNumber());
        Grade one = this.findByOne(grade);

        grade.setTeacherGrade(teacherGrade);
        grade.setTeacherMessage(message);
        if (one != null) {
            //修改
            grade.setId(one.getId());
            this.update(grade);
        } else {
            //添加
            this.insert(grade);
        }
    }
}
